package com.msb;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

/// 窗口类
public class GameFrame extends Frame {

    public static int GAME_WIDTH = 800;
    public static int GAME_HEIGHT = 600;

//    Image offScreenImage = null;

//    public Explode explode = new Explode(300,100,this);

    // 自己坦克对象
    public Tank myTank = new Tank(100,300, MyDir.DOWN,this, Group.Good);

    // 子弹对象集合
    public ArrayList<Bullet> bullets = new ArrayList<>();

    // 敌人坦克对象集合
    public ArrayList<Tank> tanks = new ArrayList<>();

    // 爆炸集合
    public ArrayList<Explode> explodes = new ArrayList<>();

    public GameFrame() {
        // 设置窗口属性
        setTitle("TK Frame");
        setBounds(500, 50, GAME_WIDTH, GAME_HEIGHT);
        setVisible(true);


        // 监听窗口事件
        addWindowListener(new WindowAdapter() {
            // 关闭
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        // 监听键盘事件
        addKeyListener(new MyKeyListener());
    }

//    @Override
//    public void update(Graphics g) {
//        if (offScreenImage == null) {
//            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
//        }
//        Graphics gOffScreen = offScreenImage.getGraphics();
//        Color c = gOffScreen.getColor();
//        gOffScreen.setColor(Color.BLACK);
//        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
//        gOffScreen.setColor(c);
//        paint(gOffScreen);
//        g.drawImage(offScreenImage, 0, 0, null);
//    }


    @Override
    public void paint(Graphics g) {
        g.drawString("子弹个数：" + bullets.size(), 100,100);
        g.drawString("敌人个数：" + tanks.size(), 100,150);

        // 画自己坦克
        myTank.paint(g);


        // 画敌人坦克
        for (int i = 0; i < tanks.size(); i++) {
            Tank tank = tanks.get(i);
            tank.paint(g);
            tank.move();
        }

        // 画子弹
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            bullet.paint(g);
        }

        // 碰撞检测(子弹和坦克检测)
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }


        // 画子弹
        for (int i = 0; i < explodes.size(); i++) {
            Explode explode = explodes.get(i);
            explode.paint(g);
        }

    }

    /// 内部类-键盘监听器
     private class MyKeyListener extends KeyAdapter {
         boolean bLeft = false;
         boolean bRight = false;
         boolean bUp = false;
         boolean bDown = false;

        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                    System.out.println("up");
                    bUp = true;
                    break;
                case KeyEvent.VK_DOWN:
                    System.out.println("down");
                    bDown = true;
                    break;
                case KeyEvent.VK_LEFT:
                    System.out.println("left");
                    bLeft = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    System.out.println("right");
                    bRight = true;
                    break;
                default:
            }

            setTkDirection();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                    bUp = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bDown = false;
                    break;
                case KeyEvent.VK_LEFT:
                    bLeft = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bRight = false;
                    break;
                case KeyEvent.VK_CONTROL:
                    System.out.println("control");
                    myTank.fire();
                    break;
                default:
                    break;
            }
            setTkDirection();
        }

        public void setTkDirection() {
            if (!bLeft && !bRight && !bUp && !bDown) {
                myTank.setMoving(false);
            } else {
                myTank.setMoving(true);
                if (bLeft) myTank.setDir(MyDir.LEFT);
                if (bRight) myTank.setDir(MyDir.RIGHT);
                if (bUp) myTank.setDir(MyDir.UP);
                if (bDown) myTank.setDir(MyDir.DOWN);
            }
        }
    }
}
